package com.coursebooking.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coursebooking.model.Course;
import com.coursebooking.model.Teacher;
import com.coursebooking.repository.CourseRepository;
import com.coursebooking.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class TeacherService extends ServiceImpl<TeacherRepository, Teacher> {

    @Autowired
    private TeacherRepository teacherRepository;
    
    @Autowired
    private CourseRepository courseRepository;
    
    @Autowired
    private CourseService courseService;

    public Teacher createTeacher(String name, String email, String phone, String department) {
        if (teacherRepository.existsByEmail(email)) {
            throw new RuntimeException("教师邮箱已存在");
        }

        Teacher teacher = new Teacher(name, email, phone, department);
        teacherRepository.insert(teacher);
        return teacher;
    }
    public Course createCourse(String name, String code, String description, Integer credit,
                               Integer hours, Long teacherId, LocalDateTime startTime,
                               LocalDateTime endTime, String location, Integer maxStudents) {
        if (courseRepository.existsByCode(code)) {
            throw new RuntimeException("课程编号已存在");
        }

        Optional<Teacher> teacherOpt = this.findById(teacherId);
        if (!teacherOpt.isPresent()) {
            throw new RuntimeException("教师不存在");
        }

        Course course = new Course(name, code, description, credit, hours,
                teacherOpt.get(), startTime, endTime, location, maxStudents);
        courseRepository.insert(course);
        return course;
    }

    public Optional<Teacher> findById(Long id) {
        return Optional.ofNullable(teacherRepository.selectById(id));
    }

    public Optional<Teacher> findByEmail(String email) {
        return teacherRepository.findByEmail(email);
    }

    public List<Teacher> findAll() {
        return teacherRepository.selectList(null);
    }

    public Teacher updateTeacher(Teacher teacher) {
        if (teacherRepository.selectById(teacher.getId()) == null) {
            throw new RuntimeException("教师不存在");
        }
        teacherRepository.updateById(teacher);
        return teacher;
    }

    public void deleteTeacher(Long id) {
        Teacher teacher = teacherRepository.selectById(id);
        if (teacher == null) {
            throw new RuntimeException("教师不存在");
        }
        
        // 手动级联删除相关课程
        List<Course> courses = courseRepository.findByTeacher(teacher);
        for (Course course : courses) {
            courseService.deleteCourse(course.getId());
        }
        
        teacherRepository.deleteById(id);
    }

    public boolean existsByEmail(String email) {
        return teacherRepository.existsByEmail(email);
    }

    public boolean existsById(Long id) {
        return teacherRepository.selectById(id) != null;
    }
}